﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ARelogRelog
{
    public partial class TimeConfigForm : Form
    {
        ARelog _pARelog;
        List<Schedule> _listSchedule = new List<Schedule>();

        public TimeConfigForm()
        {
            InitializeComponent();
        }

        public void Initialize(ARelog pARelog)
        {
            _pARelog = pARelog;
            _listSchedule = pARelog.Schedules;

            labelARelogPath.Text = pARelog.FileName;

            ReloadList();
        }

        void ReloadList()
        {
            listSchedule.Items.Clear();

            string f = "HH:mm:ss";

            foreach (Schedule s in _listSchedule)
            {
                listSchedule.Items.Add(string.Format("From: {0} ... To: {1}", s.Start.ToString(f), s.End.ToString(f)));
            }
        }

        public List<Schedule> Schedules
        {
            get
            {
                return _listSchedule;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (_pARelog == null)
                throw new ApplicationException("Form not initialized correctly");

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void TimeConfigForm_Load(object sender, EventArgs e)
        {
            pickerStart.Value = DateTime.Now;
            pickerEnd.Value = DateTime.Now;
        }

        private void listSchedule_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listSchedule.SelectedIndex > -1)
            {
                Schedule pSchedule = _listSchedule[listSchedule.SelectedIndex];

                pickerStart.Value = pSchedule.Start;
                pickerEnd.Value = pSchedule.End;
                buttonRemove.Enabled = true;
            }
            else
            {
                pickerStart.Value = DateTime.Now;
                pickerEnd.Value = DateTime.Now;
                buttonRemove.Enabled = false;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Schedule s = new Schedule(pickerStart.Value, pickerEnd.Value);

            DateTime newStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, s.Start.Hour, s.Start.Minute, s.Start.Second);
            //DateTime newEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, s.End.Hour, s.End.Minute, s.End.Second);

            foreach (Schedule s2 in _listSchedule)
            {
                DateTime tmpStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, s2.Start.Hour, s2.Start.Minute, s2.Start.Second);
                DateTime tmpEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, s2.End.Hour, s2.End.Minute, s2.End.Second);

                if (newStart >= tmpStart && newStart <= tmpEnd)
                {
                    MessageBox.Show(
                        this,
                        "Start time is in a already defined schedule",
                        "Invalid schedule",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);

                    return;
                }
            }

            _listSchedule.Add(s);

            ReloadList();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if (listSchedule.SelectedIndex > -1)
                _listSchedule.RemoveAt(listSchedule.SelectedIndex);

            ReloadList();
        }
    }
}
